﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Motoball.Paddles;

namespace Motoball.screenmanager
{
    public class MenuPaddle : MenuItem
    {
        Matrix[] transforms;
        float continuousRotation = 0;
        public PaddleAnimation animation;
        public MenuPaddle(BasicModel model, PaddleAnimation animation)
            : base(model)
        {
            this.animation = animation;
            transforms = new Matrix[model.model.Bones.Count];
        }

        public void Initialize()
        {
            //store all bone-transforms because engine uses only one real Model for each different model-file
            //so we need to reset any animation-caused transformations in SetToActive() of paddle-selection-screen
            animation.Initialize(ContentStorage.CurrentRingSize, model.model);
            model.model.CopyAbsoluteBoneTransformsTo(transforms);
            scaleFactor /= 2.8f;
        }

        public void ResetTransforms()
        {
            animation.Reset(Rotation);
            foreach (ModelBone bone in model.model.Bones)
            {
                bone.Transform = transforms[bone.Index];
            }
        }

        public override void Draw(Camera camera, GameTime gameTime)
        {
            base.Draw(camera, gameTime);
            
            animation.Draw(gameTime);
        }
        
        public void UpdateAnimation(GameTime gameTime)
        {
            continuousRotation += (float)gameTime.ElapsedGameTime.TotalSeconds/8;
            if (continuousRotation >= MathHelper.TwoPi)
                continuousRotation -= MathHelper.TwoPi;
            //update wheels
            animation.Update(gameTime, continuousRotation);
            //update rest
            animation.UpdateAnimation(gameTime);
        }

        public override void Update(GameTime gameTime, float rotationRadian)
        {
            
            base.Update(gameTime, rotationRadian);
            animation.Update(gameTime, Rotation*5f);
        }
    }
}
